﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ajeelee.Core.Database;
using Ajeelee.Core.Utils;
using FxhjpgWeb.Business;
using FxhjpgWeb.Model;
using FxhjpgWeb.WebSite.Code;

namespace FxhjpgWeb.WebSite.WebAdmin
{
    public partial class ArticleList : AuthPageBase
    {
        protected string mname = "";
        protected int cateid;
        Hashtable table = new Hashtable();
        protected IList<News> newsList = new List<News>();
        protected IList<NewsClass> newsClassList = new List<NewsClass>();
        INewsBusiness newsBll = new NewsBusinessFactory().CreateNewsBusiness();

        PageStructure ps = new PageStructure() { PageSize = 15, CurrPage = 1 };

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            try
            {
                newsClassList = Utility.GetNewsClass().Where(c => c.PId == 1).ToList();

                mname = QueryParam("mname");
                cateid = QueryParamInt32("cateid");
                ps.CurrPage = QueryParamInt32("page", 1);

                // 删除
                if (!string.IsNullOrEmpty(QueryParam("d")) && QueryParamInt32("d") > 0)
                {
                    newsBll.Delete(QueryParamInt32("d"));
                    System.Threading.Thread.Sleep(500);
                    Page_GoTo(Request.UrlReferrer.AbsoluteUri.IsNotNullAndEmpty() ? Request.UrlReferrer.AbsoluteUri : "ArticleList.aspx");
                }

                table.Add(NewsHash.Title, Function.SafeSqlLiteral(mname));
                if (cateid > 0)
                {
                    table.Add(NewsHash.CId, cateid);
                }
                else
                {
                    string cids = "";
                    foreach (var item in newsClassList)
                    {
                        cids += (cids.IsNotNullAndEmpty() ? "," : "") + item.Id;
                    }
                    if (cids.IsNotNullAndEmpty())
                    {
                        table.Add(NewsHash.CustomWhere, "N_CId in (" + cids + ")");
                    }
                }
                newsList = newsBll.GetAllNewsByPage(table, ps);
                pager1.PageStruct = ps;
            }
            catch (Exception ex)
            {
                Function.WriteErrorLog(ex);
            }
        }
    }
}